package fr.afcepf.atod20.frameWork.pattern;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import fr.afcepf.atod20.frameWork.controler.IAction;
import fr.afcepf.atod20.frameWork.formBeans.ActionForm;

/**
 * @author Stagiaire
 *
 */
public class Factory {

	private static Factory instanceFactory;

	private Map<String, IAction> mapAction = new HashMap<String, IAction>();
	private Map<String, ActionForm> mapForm = new HashMap<String, ActionForm>();
	private String nomPage;
	private static DocumentBuilderFactory dbf = DocumentBuilderFactory
			.newInstance();
	private static DocumentBuilder db;
	// private static InputStream is;
	private static Document document;

	private Factory() {

	}

	public static Factory getInstance() throws SAXException, IOException {
		if (instanceFactory == null) {
			instanceFactory = new Factory();
		}

		try {
			db = dbf.newDocumentBuilder();
			ClassLoader loader = Thread.currentThread().getContextClassLoader();
			URL resURL = loader.getResource("../frameworkAtod20KP-config.xml");
			document = db.parse(resURL.toString());

		} catch (ParserConfigurationException ex) {
			
		}
		return instanceFactory;
	}

	public ActionForm getActionForm(String formRecherche)
			throws InstantiationException, IllegalAccessException,
			ClassNotFoundException, DOMException {
		Element racine = document.getDocumentElement();
		NodeList bases = racine.getElementsByTagName("form");
		System.out.println(" nombre base "+bases.getLength());
		String nomForm = null;
		ActionForm classForm = null ;
		for( int i = 0 ;  i < bases.getLength(); i++){
			Node base =  bases.item(i);
			NodeList elements =  base.getChildNodes();
			System.out.println(" elements : " + elements.getLength());
			 for( int j= 0 ; j < elements.getLength() ; j++){
				 Node enfant = elements.item(j);
				 if(enfant.getNodeName().equals("form-name")){
					nomForm =  enfant.getTextContent();
				 }
				 if(enfant.getNodeName().equals("form-class")){
					 System.out.println(" node enfant " + enfant.getTextContent());
				 classForm = (ActionForm) Class.forName(enfant.getTextContent()).newInstance();
				 }
			 }
			 mapForm.put(nomForm, classForm);
		}
		
		return classForm;

	}

	public IAction getAction(String urlPattern) throws InstantiationException,
			IllegalAccessException, DOMException, ClassNotFoundException {

		Element element = document.getDocumentElement();
		NodeList bases = element.getElementsByTagName("action");
		String classAction = null;
		IAction monAction = null;

		for (int i = 0; i < bases.getLength(); i++) {

			Node item = bases.item(i);
			NodeList enfant = item.getChildNodes();
			String nom = null;
			String formAction = null;
			String monUrlPattern = null;
			for (int j = 0; j < enfant.getLength(); j++) {

				Node itemEnfant = enfant.item(j);

				if (itemEnfant.getNodeName().equals("action-name")) {
					nom = itemEnfant.getTextContent();

					classAction = itemEnfant.getTextContent();
					monAction = (IAction) Class.forName(classAction)
							.newInstance();
					System.out.println(monAction);
				}

				if (itemEnfant.getNodeName().equals("url-pattern")) {
					monUrlPattern = itemEnfant.getTextContent();
				}
				if (itemEnfant.getNodeName().equals("form-name")) {
					formAction = itemEnfant.getTextContent();
				}
				if(itemEnfant.getNodeName().equals("input")){
					nomPage =  itemEnfant.getTextContent();
				}
			}
			mapAction.put(nom, monAction);

		}

		return monAction;

	}

	public String getNomPage() {
		return nomPage;
	}

	public void setNomPage(String nomPage) {
		this.nomPage = nomPage;
	}

	
}
